package com.qzone;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class AddStudent extends Activity {

	Button addstudent;
	static Context mContext;
	EditText mobileno,regno;
	static EditText sipno;
	static DBHelper dbHelper;
	private static long back_pressed;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mContext=this;
		dbHelper=new DBHelper(mContext);
		if(dbHelper.getNumRows()>0)
    	{
			getActionBar().setDisplayHomeAsUpEnabled(true);
			getActionBar().setHomeButtonEnabled(true);
    	}
		
		setContentView(R.layout.activity_add_student);
		addstudent=(Button)findViewById(R.id.addstudent);
		mobileno=(EditText)findViewById(R.id.mobileno);
		regno=(EditText)findViewById(R.id.regno);
		sipno=(EditText)findViewById(R.id.sipno);
		
		addstudent.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				

				
				if(mobileno.getText().toString().equals(""))
				{
					mobileno.setError("Please fill");
					mobileno.requestFocus();
				}
				else if(regno.getText().toString().equals(""))
				{
					mobileno.setError(null);
					regno.setError("Please fill");
					regno.requestFocus();
				}
				else if(sipno.getText().toString().equals(""))
				{
					mobileno.setError(null);
					regno.setError(null);
					sipno.setError("Please fill");
					sipno.requestFocus();
				}
				else
				{
					mobileno.setError(null);
					regno.setError(null);
					
					 AsynHttp asynhttp=new AsynHttp(mContext,sipno.getText().toString(),"mob="+mobileno.getText().toString()+"&roll="+regno.getText().toString());
				     asynhttp.execute(AsynHttp.STDADD);
					
				}
				
			}
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		return true;
	}

	@Override
	public void onBackPressed()
	{
		if(dbHelper.getNumRows()>0)
    	{
			Intent intent=new Intent(mContext,StudentList.class);
			startActivity(intent);
			finish();
    	}
		else
		{
			 if (back_pressed + 2000 > System.currentTimeMillis()) super.onBackPressed();
		     else Toast.makeText(getBaseContext(), "Press once again to exit!", Toast.LENGTH_SHORT).show();
		     back_pressed = System.currentTimeMillis();
		}
	}
	
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == android.R.id.home) {
			Intent intent=new Intent(mContext,StudentList.class);
			startActivity(intent);
			finish();
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
	
	public static void stdAddResponse(String res){
		
		if (res != null)
		{
			try {
				JSONObject jsonObj = new JSONObject(res);
				if(jsonObj.get("status").equals("2"))
				{
					Toast.makeText(mContext, jsonObj.get("error_message").toString(), Toast.LENGTH_LONG).show();	
				}
				else if(jsonObj.get("status").equals("1"))
				{
					JSONArray jsonArr=jsonObj.getJSONArray("studentdetails");
					JSONObject jsonstd=jsonArr.getJSONObject(0);
					
					long ressult=dbHelper.insertData(sipno.getText().toString(),jsonstd.getString("student_masterid"),jsonstd.getString("student_school_name"),jsonstd.getString("student_name"),jsonstd.getString("student_rollno"),jsonstd.getString("student_mobileno"),jsonstd.getString("student_tagno"));
					if(ressult>0)
					{
						Intent studentlistintent=new Intent(mContext,StudentList.class);
						mContext.startActivity(studentlistintent);
						((Activity) mContext).finish();
					}
					
					
					
					//Toast.makeText(mContext, jsonstd.getString("student_masterid")+"--"+jsonstd.getString("student_school_name")+"--"+jsonstd.getString("student_name")+"--"+jsonstd.getString("student_rollno")+"--"+jsonstd.getString("student_mobileno")+"--"+jsonstd.getString("student_tagno"), Toast.LENGTH_LONG).show();
				}
				
			}
			catch (JSONException e) {
				e.printStackTrace();
			}
		}
		else 
		{
			Log.e("ServiceHandler", "Couldn't get any data from the url");
		}
		
		
		
	}
}
